/**
 * 功能描述
 */
define(function (require, exports, module) {
	var Grid = require("grid");
	var Dialog = require("dialog");
	var aw = require("ajaxwrapper");
	
	var roomHandler = require("../handler/roomHandler");
	
    var instance = {
        initComponent: function (params,widget) {
        	var grid = new Grid({
        		parentNode: ".J-grid",
        		model:{
        			url:"api/roompersonalInfo/query",
        			autoRender: false,
        			params:function(){
        				var subnav = widget.get("subnav");
        				return {
        					"checkInImplement" : widget.get("pkCheckInImplement"),
        					fetchProperties:"pkRoomPersonalInfo," +
    							"roomRegister.room.pkRoom," +
    							"roomRegister.room.code," +
    							"roomRegister.status," +
    							"checkInImplement.groupReserve.checkInDate," + 
    							"checkInImplement.groupReserve.checkOutDate," +
    							"checkInImplement.status," +
    							"roomRegister.pkRoomRegister," +
    							"roomRegister.version," +
    							"roomRegister.cardKey," +
    							"roomRegister.water," +
    							"roomRegister.electric," +
    							"roomRegister.gas," +
    							"roomRegister.telephone," +
    							"roomRegister.wireless," +
    							"roomRegister.pendDescription," +
    							"descript"	
        				};
        			},
        			head : {
						items : [ {
							id : "setAll",
							type : "button",
							text : "全部落实",
							handler : function() {
								var gridData = widget.get("grid").getData();
								
								var flag = true;
								
								for(var i in gridData){
									if(gridData[i].roomRegister.status.key == "Pending"){
										flag = false;
									}
								}
								if(!flag){
									aw.ajax({
										url : "api/roomregister/updatestatus",
										data : {
											pkCheckInImplement : widget.get("pkCheckInImplement"),
										},
										dataType : "json",
										success : function(datas){
											widget.get("grid").refresh();
										}
									})
								}else{
									Dialog.alert({
										tip : "提示",
										content : "该团所有房间都已落实！"
									});
									return false;
								}
							}
						} ]
					},
        			columns:[{
        				name:"roomRegister.room.code",
        				label:"房间",
        			},{
        				name:"checkInImplement.groupReserve.checkInDate",
        				label:"入住日期",
        				format:"date"
        			},{
        				name:"checkInImplement.groupReserve.checkOutDate",
        				label:"退住日期",
        				format:"date"
        			},{
        				name:"descript",
        				label:"备注"
        			},{
        				name:"roomRegister.status.value",
        				label:"状态",
        			},{
        				name:"show",
        				label:"操作",
        				format:"button",
        				formatparams:[{
        					id:"Pended",
        					text:"落实",
        					show:function(value,row){
        						if(row.roomRegister.status && row.roomRegister.status.key == "Pended"){
        							return false;
        						}else{
        							return true;
        						}
        					},
        					handler:function(index,data,rowEL){
        						roomHandler.pendDataHandler(data, widget, true);
        					}
        				},{
        					id:"check",
        					text:"查看",
        					show:function(value,row){
        						if(row.roomRegister.status && row.roomRegister.status.key == "Pended"){
        							return true;
        						}else{
        							return false;
        						}
        					},
        					handler:function(index,data,rowEL){
        						roomHandler.pendDataHandler(data, widget, false);
        					}
        				}]
        			}]
        		}
        	});
        	widget.set("grid",grid);
        }
    };
    module.exports = instance;
});
